package org.wisdom.ab.domain.staff.repository.dao.impl;

import org.wisdom.ab.domain.staff.entity.AbStaff;
import org.wisdom.ab.domain.staff.repository.dao.StaffDao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

/**
 * 成员仓储扩展类
 *
 * @author zhangzhiyan
 * @date 2022/05/05 13:39
 **/
@SuppressWarnings("unused")
public class StaffDaoImpl implements StaffDao {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<AbStaff> findAllByDeptId(String departmentId) {
        String jpql =
            "select u from AbStaff u,AbStaffDepartment sd where u.id = sd.staffId and sd.deptId = ?1 order by sd.order asc ";
        TypedQuery<AbStaff> query = this.entityManager.createQuery(jpql, AbStaff.class);
        query.setParameter(1, departmentId);
        return query.getResultList();
    }

    @Override
    public long countByDeptId(String departmentId) {
        String jpql =
            "select count(u.id) from AbStaff u,AbStaffDepartment sd where u.id = sd.staffId and sd.deptId = ?1 order by sd.order asc ";
        TypedQuery<Long> num = this.entityManager.createQuery(jpql, Long.class);
        return num.getFirstResult();
    }
}
